﻿using System;
using System.Xml;
using System.Xml.XPath;
using System.Reflection;

namespace Landscape.XSLT {
    public class BaseExtension {
        /// <summary>
        /// Dumps all the feature of xslt library, along with usage parameters
        /// </summary>
        /// <returns></returns>
        public XPathNodeIterator GetFeatures() {
            var doc = new XmlDocument() { InnerXml = "<features/>" };
            MethodInfo[] methods = this.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            if (methods.Length > 0) {
                foreach (MethodInfo m in methods) {
                    if (m.IsStatic || !m.IsPublic || m.IsConstructor) continue;
                    string parameterString = "";
                    ParameterInfo[] parameters = m.GetParameters();
                    if (parameters.Length > 0) {
                        foreach (ParameterInfo p in parameters) {
                            if (!string.IsNullOrEmpty(parameterString)) parameterString += ", ";
                            parameterString += p.ParameterType.Name.ToLower() + " " + p.Name;
                        }
                    }

                    var node = doc.CreateElement("feature");
                    node.InnerText = String.Format("{0} {1}({2});", m.ReturnType.Name.ToLower(), m.Name, parameterString);
                    XmlNode feat = doc.DocumentElement.AppendChild(node);
                }
            }

            return doc.DocumentElement.CreateNavigator().Select("child::feature");
        }
    }
}
